package stack;


/**
 * @author huangxin
 * @date 2019/6/21-13:21
 */
public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(5);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.list();

    }

}

class ArrayStack {
    //栈的大小
    private int maxSize;
    //数组模拟栈
    private int[] stack;
    //top表示栈顶
    private int top = -1;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    public boolean isFill() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void push(int ele) {
        if (isFill()) {
            System.out.println("栈已满");
            return;
        }
        top++;
        stack[top] = ele;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        int result = stack[top];
        top--;
        return result;
    }

    public void list() {
        if (isEmpty()) {
            System.out.println("栈为空,没用数据");
        }
        //从栈顶开始遍历
        for (int i = top; i >=0 ; i--) {
            System.out.printf("stack[%d] = %d\n",i,stack[i]);
        }
    }



}